package okhttp3Demo.demo;

import com.alibaba.fastjson.JSON;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3Demo.OkHttpClientFactory;
import utils.DateUtils;
import utils.file.FileDownloadUtils;

import java.io.File;

public class YanFenFen {

    public static void main(String[] args) {
        try {
            String dir = "/Users/lining/Desktop/YanFenFen/" + DateUtils.formatDate("yyyyMMdd_HHmmss");
            String excel = "/Users/lining/Desktop/YanFenFen/data.xls";
            new File(dir).mkdir();
            Sheet sheet  = Workbook.getWorkbook(new File(excel)).getSheet(0);
            StringBuilder info = new StringBuilder("文件路径：").append(dir).append("\n")
                    .append("总记录数：").append(sheet.getRows()).append("\n");
            StringBuilder failInfo = new StringBuilder();

            Cell[] cells = null;
            for(int r =0 ;r<sheet.getRows();r++){
                try {
                    cells = sheet.getRow(r);
                    String itemNum = cells[0].getContents().trim();
                    String name = cells[1].getContents().trim();
                    String tempInfo = String.format("itemNum=%s,name=%s:",itemNum,name);

                    ResponseBody body = getImage(itemNum);
                    if(body.getCode() != 0){
                        failInfo.append(tempInfo).append(String.format("失败：code=%s,message=%s\n",body.getCode(),body.getMessage()));
                        continue;
                    }

                    String subDir = dir + File.separator + itemNum;
                    String baseFileName = subDir + File.separator + name +"后台日志1";
                    String projectFileName = subDir + File.separator + name + "后台日志2";
                    new File(subDir).mkdir();

                    boolean haveBase = body.getData().getBase() != null && !"".equals(body.getData().getBase());
                    boolean haveProject= body.getData().getProject() != null && !"".equals(body.getData().getProject());

                    if(haveBase && haveProject)info.append(tempInfo).append("成功。\n");
                    else if(!haveBase && haveProject)failInfo.append(tempInfo).append(String.format("失败：后台日志1文件为空\n"));
                    else if(haveBase && !haveProject)failInfo.append(tempInfo).append(String.format("失败：后台日志2文件为空\n"));
                    else failInfo.append(tempInfo).append(String.format("失败：后台日志1文件为空,后台日志2文件为空\n"));

                    if (haveBase) FileDownloadUtils.downloadFile(body.getData().getBase(), baseFileName);
                    if (haveProject) FileDownloadUtils.downloadFile(body.getData().getProject(), projectFileName);
                } catch (Exception e) {
                    failInfo.append(String.format("第%s行执行异常\n",r+1));
                    e.printStackTrace();
                }
            }
            System.out.println(info.append(failInfo).toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static ResponseBody getImage(String itemNum) throws Exception{
        StringBuilder sb = new StringBuilder("https://lmd.qianshengqian.com/default/export?apply_code=").append(itemNum);
        OkHttpClient client = OkHttpClientFactory.getInstance().getClient(OkHttpClientFactory.EMI_CALL_CLIENT);
        Request request = new Request.Builder().url(sb.toString()).build();
        Response response = client.newCall(request).execute();
        String resultStr = response.body().string();
        return JSON.parseObject(resultStr,ResponseBody.class);
    }

}
